package com.webservice.test;


import com.webservice.model.User;
import com.webservice.service.UserService;
import com.webservice.service.impl.UserServiceImpl;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;

@RunWith(SpringRunner.class)
@SpringBootTest
public class test {

    private Logger logger = LoggerFactory.getLogger(test.class);

    @Test
    public void contextLoads() {
    }

    /***
     * 方法一：动态客户端调用
     */
    @Test
    public void DynamicClient() {

        JaxWsDynamicClientFactory jwdcf = JaxWsDynamicClientFactory.newInstance();

        Client client = jwdcf.createClient("http://localhost:8081/service/user?wsdl");

        Object[] objects = new Object[0];

        try {
            objects = client.invoke("getAllUser");
            logger.info("获取user信息===》{}", objects[0].toString());
            System.out.println("invoke实体：" + ((ArrayList) objects[0]).get(0).getClass().getPackage());
            for (int i = 0; i < ((ArrayList) objects[0]).size(); i++) {
                User user = new User();
                BeanUtils.copyProperties(((ArrayList) (objects[0])).get(i), user);
                logger.info("DynamicClient方式，获取用户信息==》ID：{}，姓名：{}，年龄：{}，更新时间：{}", user.getUserId(), user.getUsername(), user.getAge(), user.getUpdateTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void ProxyFactory() {
        String address = "http://localhost:8081/service/user?wsdl";
        //代理工厂
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        //设置代理地址
        factoryBean.setAddress(address);
        //设置接口类型
        factoryBean.setServiceClass(UserService.class);
        //创建一个代理接口实现
        UserService userService = (UserService) factoryBean.create();

        ArrayList<User> users = userService.getAllUser();

        users.forEach(user -> {
            logger.info("ProxyFactory方式,获取用户{}信息 ==》用户姓名：{},年龄：{},创建时间：{}", user.getUserId(), user.getUsername(), user.getAge(), user.getUpdateTime());
        });
    }

}
